package com.thinkfit.jy.fragment;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.common.lib.constant.EventBusEvent;
import com.common.lib.fragment.BaseFragment;
import com.common.lib.manager.DataManager;
import com.common.lib.mvp.contract.EmptyContract;
import com.common.lib.mvp.presenter.EmptyPresenter;
import com.thinkfit.jy.R;
import com.thinkfit.jy.activity.BrandInfoActivity;
import com.thinkfit.jy.activity.VersionActivity;
import com.thinkfit.jy.utils.DeviceTypeUtils;
import com.thinkfit.jy.utils.SystemUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;

/**
 * 关于界面
 */
public class DeviceInfoFragment extends BaseFragment<EmptyContract.Presenter> implements EmptyContract.View {

    TextView tvSystemVersion,tvResolution,tvRam,tvStorage;
    LinearLayout llSportBack;
    @NonNull
    @Override
    protected EmptyContract.Presenter onCreatePresenter() {
        return new EmptyPresenter(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_device_info;
    }

    @Override
    protected void initView(@NonNull View view, @Nullable Bundle savedInstanceState) {
        llSportBack = view.findViewById(R.id.ll_sport_back);
        tvSystemVersion = view.findViewById(R.id.tv_system_version);
        tvResolution = view.findViewById(R.id.tv_resolution);
        tvRam = view.findViewById(R.id.tv_ram);
        tvStorage = view.findViewById(R.id.tv_storage);

        llSportBack.setOnClickListener(this);


        getStorage();
        getResolution();
        getRam();
        getSystemVersion();
    }

    public void getResolution(){
        DisplayMetrics displayMetrics = new DisplayMetrics();
        WindowManager windowManager = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
        windowManager.getDefaultDisplay().getMetrics(displayMetrics);

        int width = displayMetrics.widthPixels;
        int height = displayMetrics.heightPixels;

        tvResolution.setText( width+"*"+height);
    }

    public void getStorage(){
        long availableExternalMemorySize = SystemUtils.getAvailableExternalMemorySize();
        long totalExternalMemorySize = SystemUtils.getTotalExternalMemorySize();
        String available = SystemUtils.bytesToGb(availableExternalMemorySize);
        String total = SystemUtils.bytesToGb(totalExternalMemorySize);
        tvStorage.setText(available+"/"+total);
    }

    public void getRam(){
        long totalMemory = SystemUtils.getTotalMemory();
        long availableMemory = SystemUtils.getAvailableMemory(getContext());
        long used = totalMemory-availableMemory;
        String s = SystemUtils.bytesToGb(used);
        String s1 = SystemUtils.bytesToGb(totalMemory);
        tvRam.setText(s+"/"+s1);
    }

    public void getSystemVersion(){
        PackageManager packageManager = getContext().getPackageManager();
        try {
            // 获取当前应用程序的包名
            String packageName = getContext().getPackageName();

            // 根据包名获取应用程序的版本信息
            PackageInfo packageInfo = packageManager.getPackageInfo(packageName, 0);

            // 输出版本号
            String versionName = packageInfo.versionName;
            tvSystemVersion.setText("Fiter OS " + versionName);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            tvSystemVersion.setText("Fiter OS Unknown");
        }
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_sport_back:
                HashMap<String, Object> map = new HashMap<>();
                map.put(EventBusEvent.TO_FRAGMENT,3);
                EventBus.getDefault().post(map);
                break;
        }
    }
}
